﻿
using TraumaCenterSystem.Bll;
using TraumaCenterSystem.Core;
using TraumaCenterSystem.TraumaCenterManage;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace TraumaCenterSystem.Code.Bll
{
    public class PathwayManager : BaseManager
    {
        /// <summary>
        /// 获取诊疗路径列表
        /// </summary>
        /// <param name="pathName"></param>
        /// <returns></returns>
        public static OperateResult GetDiagnosticPath(int pageIndex,int pageSize, string pathName)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                TraumaCenterManageSoapClient service = new TraumaCenterManageSoapClient();
                ResultHelperOfString result = service.GetDiagnosticPath(pathName, pageIndex, pageSize);
                if (result.S)
                {
                    resultInfo.Body = result.D;
                }
                else
                {
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }



        /// <summary>
        /// 获取诊疗路径名称列表
        /// ID Name 
        /// </summary>
        /// <returns></returns>
        public static OperateResult GetPathNameList()
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                TraumaCenterManageSoapClient service = new TraumaCenterManageSoapClient();
                ResultHelperOfString result = service.GetPathNameList();
                if (result.S)
                {
                    resultInfo.Body = result.D;
                }
                else
                {
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }            
            return resultInfo;
        }

        /// <summary>
        /// 新增或编辑诊疗路径 依据ID
        /// </summary>
        /// <param name="pathJson"></param>
        /// <returns></returns>
        public static OperateResult SavePathName(string pathJson) 
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                TraumaCenterManageSoapClient service = new TraumaCenterManageSoapClient();               
                ResultHelperOfBoolean result = service.SetDiagnosticPath(pathJson);
                if (result.S)
                {
                    resultInfo.Body = result.M;
                }
                else
                {
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }

            return resultInfo;
        }

        /// <summary>
        /// 返回诊疗路径对象
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static OperateResult GetDiagnosticPathById(int Id) 
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                TraumaCenterManageSoapClient service = new TraumaCenterManageSoapClient();
                ResultHelperOfString result = service.GetDiagnosticPathById(Id);
                if (result.S)
                {
                    resultInfo.Body = result.D;
                }
                else
                {
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }

            return resultInfo;
        }

        /// <summary>
        /// 根据诊疗路径ID获取模板或内容
        /// </summary>
        /// <param name="pathId"></param>
        /// <returns></returns>
        public static OperateResult GetDiagnosticPathTemp(int pathId, string patientId) 
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                TraumaCenterManageSoapClient service = new TraumaCenterManageSoapClient();
                ResultHelperOfString result = service.GetDiagnosticPathTemp(pathId, patientId);
                if (result.S)
                {
                    resultInfo.Body = result.D;
                }
                else
                {
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 保存诊疗路径模板
        /// </summary>
        /// <param name="contentJson"></param>
        /// <returns></returns>
        public static OperateResult SetDiagnosticPathTemp(string contentJson) 
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                TraumaCenterManageSoapClient service = new TraumaCenterManageSoapClient();
                ResultHelperOfBoolean result = service.SetDiagnosticPathTemp(contentJson);
                if (result.S)
                {
                    resultInfo.Body = result.D;
                }
                else
                {
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }



        /// <summary>
        /// 获取质控列表（所有）
        /// </summary>
        /// <param name="pathName"></param>
        /// <returns></returns>
        public static OperateResult GetQualityPointList()
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                string NameSelect = @"use TraumaCenterManage; 
                            SELECT * FROM [QualityPointSet] where  Status <> 2 order by Sort ";
                DataSet NameList = SQLDLL.SqlHelper.ExecuteDataset(NameSelect, System.Data.CommandType.Text);


                if (NameList != null && NameList.Tables != null && NameList.Tables.Count>0)
                {
                    DataTable dt = NameList.Tables[0];
                    resultInfo.Body = dt;
                }
                else
                {
                    resultInfo.Body = "查询失败";
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 保存质控项
        /// </summary>
        /// <param name="pathName"></param>
        /// <returns></returns>
        public static OperateResult SaveQualityPoint(string Id, string QualityPointTitle, string ComparePointId, string OverTime, string Sort, string Status, string SetUserId)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                string NameSelect = @"use TraumaCenterManage; 
                            update [QualityPointSet] set  ComparePointId=" + ComparePointId + @",
                            ComparePointTitle=(select QualityPointTitle from QualityPointSet where Id =" + ComparePointId + @"),
                            ComparePointName =(select QualityPointName from QualityPointSet where Id =" + ComparePointId + @"),
                            ComparePointTableName =(select QualityPointTableName from QualityPointSet where Id =" + ComparePointId + @"),
                            OverTime = " + OverTime + @",Sort =  " + Sort + @",Status =  " + Status + @",
                            SetUserId = '"+SetUserId +@"'
                            where Id=" + Id ;
                int rst = SQLDLL.SqlHelper.ExecuteNonQuery(System.Data.CommandType.Text,NameSelect );


                if (rst > 0)
                {
                    resultInfo.Body = "保存成功";
                }
                else
                {
                    resultInfo.Body = "保存失败";
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }


        /// <summary>
        /// 保存质控项
        /// </summary>
        /// <param name="pathName"></param>
        /// <returns></returns>
        public static OperateResult ChangeStatus(string Id, string Status)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                string NameSelect = @"use TraumaCenterManage; 
                            update [QualityPointSet] set Status =  " + Status + " where Id=" + Id;
                int rst = SQLDLL.SqlHelper.ExecuteNonQuery(System.Data.CommandType.Text, NameSelect);


                if (rst > 0)
                {
                    resultInfo.Body = "保存成功";
                }
                else
                {
                    resultInfo.Body = "保存失败";
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }


        /// <summary>
        /// 获取已开启的质控项列表
        /// ID Name 
        /// </summary>
        /// <returns></returns>
        public static OperateResult GetOpenQuanlityPointList(string PatientID)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                TraumaCenterManageSoapClient service = new TraumaCenterManageSoapClient();
                ResultHelperOfDataCompleteTimeLine result = service.GetTimeline(PatientID);
                if (result.S)
                {
                    resultInfo.Body = result.D;
                }
                else
                {
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }


    }
}